package mygang.dao.simple;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import mygang.dao.DAO;
import org.apache.log4j.Logger;

/**
 *
 * @author Volodymyr Lavrynovych <volodya.lavrynovych@gmail.com>
 * date: 09/01/2013
 */
public abstract class BaseDAO extends DAO {
    
    protected static Logger log = Logger.getLogger(BaseDAO.class.toString());
    protected static Connection conn = null;
    
    /**
     * Returns connection
     * @return connection
     * @throws ClassNotFoundException
     * @throws SQLException 
     */
    private static Connection getConnection() throws SQLException, ClassNotFoundException {
        Class.forName(getProperty(DRIVER));
        return DriverManager.getConnection(getProperty(SERVER)+"/"+getProperty(NAME), getProperty(LOGIN), getProperty(PASSWORD));
    }
    
    public static void open() throws SQLException{
        try {
            conn = getConnection();
            if(conn!=null) log.debug("Connection was opened...");
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
    }
    
    public static void close(){
        try {
            if(conn!=null) conn.close();
            if(conn==null) log.debug("Connection was closed...");
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
    

}